package com.archive.api;

import com.utils.DateUtil;
import com.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 日常档案
 */
@WebServlet(name="UpdateDailyArchive",urlPatterns={"/UpdateDailyArchive"})
public class UpdateDailyArchive extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileNumber = req.getParameter("fileNumber");   //档案编号
        String fileTitle = req.getParameter("fileTitle");     //档案名称
        String fillingDateStr = req.getParameter("fillingDate");  //填写日期
        Date fillingDate = DateUtil.defaultParse(fillingDateStr);
        String userId = req.getParameter("userId");               //用户编号
        String animalId = req.getParameter("animalId");           //动物编号
        String dailyDataTip = req.getParameter("dailyDataTip");   //信息备注
        String file = req.getParameter("file");
        Connection con = JdbcUtils.getCon();
        PreparedStatement stat = null;
        try {
            String sql = "update animalfile09_dailyarchive set FileTitle=?, FillingDate=?, UserID=?, AnimalID=?," +
                    " DailyDataTip=?,FileName=? where FileNumber = ?";
            stat = con.prepareStatement(sql);
            stat.setString(1, fileTitle);
            stat.setTimestamp(2, new Timestamp(fillingDate.getTime()));
            stat.setString(3, userId);
            stat.setString(4, animalId);
            stat.setString(5, dailyDataTip);
            stat.setString(6, file);
            stat.setString(7, fileNumber);
            int i = stat.executeUpdate();
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("操作错误！！！");
            resp.getWriter().println(e.getMessage());
            return;
        } finally {
            JdbcUtils.close2(con, stat);
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().println("{\"msg\": \"OK\"}");

    }

}
